package com.videogo.ui.message;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ciban.hkcamera.R;
import com.videogo.openapi.bean.resp.AlarmInfo;
import com.videogo.universalimageloader.core.DisplayImageOptions;
import com.videogo.universalimageloader.core.ImageLoader;
import com.videogo.universalimageloader.core.assist.FailReason;
import com.videogo.universalimageloader.core.download.DecryptFileInfo;
import com.videogo.universalimageloader.core.listener.ImageLoadingListener;
import com.videogo.universalimageloader.core.listener.ImageLoadingProgressListener;

public class MessageListAdapter extends ArrayAdapter<AlarmInfo> {
    private class ViewHolder {
        TextView timeText;
        ImageView image;
        TextView from;
        TextView type;
        ViewGroup contentLayout;
        ViewGroup videoLayout;
        ViewGroup playLayout;
        ViewGroup playVideoLayout;
        ProgressBar imageProgress;
    }

    /** 监听对象 */
    private OnClickListener mListener;
    private ImageLoader mImageLoader;

    public MessageListAdapter(Context context, List<AlarmInfo> alarmInfoList) {
        super(context, 0, alarmInfoList);
        mImageLoader = ImageLoader.getInstance();
    }

    public void setOnClickListener(OnClickListener l) {
        mListener = l;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.message_list_item, parent, false);

            // 获取控件对象
            viewHolder.timeText = (TextView) convertView.findViewById(R.id.message_time);
            viewHolder.contentLayout = (ViewGroup) convertView.findViewById(R.id.message_content);
            viewHolder.image = (ImageView) convertView.findViewById(R.id.message_image);
            viewHolder.from = (TextView) convertView.findViewById(R.id.message_from);
            viewHolder.type = (TextView) convertView.findViewById(R.id.message_type);
            viewHolder.playLayout = (ViewGroup) convertView.findViewById(R.id.message_play_layout);
            viewHolder.videoLayout = (ViewGroup) convertView.findViewById(R.id.message_video_layout);
            viewHolder.playVideoLayout = (ViewGroup) convertView.findViewById(R.id.play_video_layout);
            viewHolder.imageProgress = (ProgressBar) convertView.findViewById(R.id.message_image_progress);
            
            // 防止获得缓存
            viewHolder.image.setDrawingCacheEnabled(false);
            viewHolder.image.setWillNotCacheDrawing(true);

            // 内容区域的点击响应
            viewHolder.contentLayout.setOnClickListener(mOnClickListener);
            // 点击图片区域
            viewHolder.image.setOnClickListener(mOnClickListener);
            viewHolder.videoLayout.setOnClickListener(mOnClickListener);
            viewHolder.playLayout.setOnClickListener(mOnClickListener);

            // 设置控件集到convertView
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // 设置position
        viewHolder.contentLayout.setTag(position);
        viewHolder.image.setTag(position);
        viewHolder.videoLayout.setTag(position);
        viewHolder.playLayout.setTag(position);

        viewHolder.imageProgress.setProgress(0);
        viewHolder.imageProgress.setVisibility(View.VISIBLE);
        // 获取数据
        AlarmInfo alarmLogInfo = getItem(position);

        if (alarmLogInfo != null) {
            // 消息类型
            // TODO 后面将在赋值时就转换AlarmType
            AlarmType alarmType = AlarmType.getAlarmTypeById(alarmLogInfo.getAlarmType());

            viewHolder.type.setText(getContext().getString(alarmType.getTextResId()));

            // 消息来源
            viewHolder.from.setText(alarmLogInfo.getAlarmName());

            // 消息时间
            viewHolder.timeText.setText(alarmLogInfo.getAlarmStart());

            // 消息图片
            if (alarmType.hasCamera()) {
                //viewHolder.image.setImageResource(R.drawable.my_cover);
                loadImage(viewHolder, alarmLogInfo);
            } else {
                mImageLoader.cancelDisplayTask(viewHolder.image);
                viewHolder.image.setImageResource(alarmType.getDrawableResId());
                viewHolder.imageProgress.setVisibility(View.GONE);
            }

            if (alarmType.hasCamera()) {
                viewHolder.playLayout.setEnabled(true);
                viewHolder.videoLayout.setEnabled(true);
                viewHolder.playVideoLayout.setVisibility(View.VISIBLE);
            } else {
                viewHolder.playVideoLayout.setVisibility(View.GONE);
            }
        }

        return convertView;
    }

    private void loadImage(final ViewHolder viewHolder, AlarmInfo alarmLogInfo) {
        viewHolder.image.setBackgroundDrawable(null);
        viewHolder.image.setImageResource(R.mipmap.notify_bg);
        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
                .needDecrypt(alarmLogInfo.getAlarmEncryption()).considerExifParams(true)
                .showImageForEmptyUri(R.mipmap.event_list_fail_pic).showImageOnFail(R.mipmap.event_list_fail_pic)
                .showImageOnDecryptFail(R.mipmap.alarm_encrypt_image_mid)
                .extraForDownloader(new DecryptFileInfo(alarmLogInfo.getDeviceSerial(), alarmLogInfo.getCheckSum()))
                .build();

        mImageLoader.displayImage(alarmLogInfo.getAlarmPicUrl(), viewHolder.image, options, new ImageLoadingListener() {

            @Override
            public void onLoadingStarted(String imageUri, View view) {
                viewHolder.imageProgress.setProgress(0);
                viewHolder.imageProgress.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                viewHolder.imageProgress.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                viewHolder.imageProgress.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                viewHolder.imageProgress.setVisibility(View.GONE);
            }

        }, new ImageLoadingProgressListener() {

            @Override
            public void onProgressUpdate(String imageUri, View view, int current, int total) {
                viewHolder.imageProgress.setProgress(current * 100 / total);
            }
        });
    }
    
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                int position;
                int i = v.getId();
                if (i == R.id.message_video_layout) {
                    position = (Integer) v.getTag();
                    mListener.onVideoButtonClick(MessageListAdapter.this, v, position);

                } else if (i == R.id.message_play_layout) {
                    position = (Integer) v.getTag();
                    mListener.onPlayButtonClick(MessageListAdapter.this, v, position);

                } else if (i == R.id.message_content) {
                    position = (Integer) v.getTag();
                    mListener.onItemClick(MessageListAdapter.this, v, position);

                } else if (i == R.id.message_image) {
                    position = (Integer) v.getTag();
                    AlarmInfo alarmLogInfo = getItem(position);
                    if (alarmLogInfo != null && AlarmType.getAlarmTypeById(alarmLogInfo.getAlarmType()).hasCamera()) {
                        mListener.onImageClick(MessageListAdapter.this, v, position);
                    }

                }
            }
        }
    };

    public void clearImageCache() {
        mImageLoader.clearMemoryCache();
    }

    public interface OnClickListener {
        public void onItemClick(BaseAdapter adapter, View view, int position);

        public void onImageClick(BaseAdapter adapter, View view, int position);

        public void onVideoButtonClick(BaseAdapter adapter, View view, int position);

        public void onPlayButtonClick(BaseAdapter adapter, View view, int position);
    }
}